package com.yvan.serverless;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(name = "serverless.design.url")
@EnableConfigurationProperties(ServerLessProperties.class)
@Slf4j
public class ServerLessDesignAutoConfiguration {

    @Autowired
    public ServerLessProperties properties;

    @Bean
    public ServerLessDesignServlet serverLessDesignServlet() {
        return new ServerLessDesignServlet();
    }


    /**
     * 注册 Servlet
     */
    @Bean
    public ServletRegistrationBean<ServerLessDesignServlet> getServletDesign() {
        String url = properties.getDesign().getUrl() + "/*";
        log.info("\r\n================================\r\n= DesignServerLess \"{}\"\r\n================================", url);
        return new ServletRegistrationBean(serverLessDesignServlet(), url);
    }

    @Bean
    public DesignService designService() {
        return new DesignService(properties);
    }
}
